package com.codingfuture.web.controller;

import com.codingfuture.entity.Person;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author kobe
 * @Date 2021/11/4 10:01
 * @Description
 */
@Controller
@RequestMapping("/emp")  //窄化请求
public class TestController {


    //handler
    //localhost:8080/emp/findAll?count=1
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)  //deptFindAll
    public String hello(@RequestParam(value = "id", required = false, defaultValue = "3") Integer id) {
        System.out.println(id);
        System.out.println("hello SpringMVC!");
        return "index";   //  index.jsp
    }

    //localhost:8080/demo1?username=zs&password=123&birthday=2021-12-09
    @RequestMapping("/demo1")
    public String demo1(Person person) {
        System.out.println(person.getUsername());
        System.out.println(person.getPassword());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //System.out.println(simpleDateFormat.format(person.getBirthday()));
        return "index";
    }

    //localhost:8080/demo2?ids=10&ids=20&ids=23
    @RequestMapping("/demo2")
    public String demo2(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        return "index";
    }

    //localhost:8080/demo3?list=10&list=20&list=23
    @RequestMapping("/demo3")
    public String demo3() {
        System.out.println("demo3");
//        System.out.println(list);
        return "index";
    }

    //localhost:8080/demo4?username=zs
    @RequestMapping("/demo4")
    public String demo4(HttpServletRequest request,
                        HttpServletResponse response,
                        HttpSession session) throws IOException {
        System.out.println(request.getContextPath());
        System.out.println(request.getParameter("username"));
        session.setAttribute("msg", "200");
        response.getWriter().write("123");
        return "index";
    }

    @RequestMapping(value = "/demo5", method = RequestMethod.GET)
    public List<Person> demo5(HttpSession session) {
        System.out.println(session.getAttribute("msg"));
//        return "forward:/demo3";
        //return "redirect:/demo3";
        //return "redirect:/index1.jsp";
        return null;
    }

    //localhost:8080/emp/person/2
    //@RequestMapping(value = "/demo6",method = )
//    @GetMapping("/demo6")
//    @PostMapping
    public String demo6(@PathVariable Integer id) {
        System.out.println(id);//2
        return "index";
    }


}
